package com.qiuzhiyi.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.qiuzhiyi.Const;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.AllowanceAdapter;
import com.qiuzhiyi.base.QZYBaseFragment;
import com.qiuzhiyi.item.AllowanceItem;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description: 返费.推荐榜
 */

public class AllowanceListFragment extends QZYBaseFragment {


    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
//    @BindView(R.id.txt_recommend)
//    TextView mTxtRecommend;

    private AllowanceAdapter mAdapter;
    private List<AllowanceItem.ListBean> mListBeans;
    private String mType;

    public static AllowanceListFragment newInstance(String type) {

        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        AllowanceListFragment fragment = new AllowanceListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_allowance_list;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mAdapter = new AllowanceAdapter(R.layout.item_allwoance);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter.bindToRecyclerView(mRecycler);
        mAdapter.setEmptyView(R.layout.layout_empty_fee);
        setSmartRefreshLayout(mRefreshLayout);
        if (getArguments() == null) {
            return;
        }
        mType = getArguments().getString("type");
//        mTxtRecommend.setVisibility(mType.equals("1") ? View.GONE : View.VISIBLE);
        getFeeList("1");
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        getFeeList("1");
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        getFeeList(String.valueOf(mPage + 1));
    }

    /**
     * 获取列表
     *
     * @param page 页码
     */
    private void getFeeList(String page) {
        Map<String, String> map = new HashMap<>();
        map.put("fType", mType);
        map.put("pageNum", page);
        map.put("numPerPage", Const.NUM_PER_PAGE);
        ObservableHelper.createObservable(RetrofitHelper.createService().getFeeList(map))
                .subscribe(new SelectorObserver<BaseItem<AllowanceItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onSuccess(BaseItem<AllowanceItem> item) {
                        stopRefreshLoad();
                        List<AllowanceItem.ListBean> tmp = item.getObjValue().getList();
                        if (mIsRefresh) {
                            mPage = 1;
                            mListBeans = tmp;
                            mAdapter.setNewData(mListBeans);

                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mRefreshLayout.setLoadmoreFinished(false);
                            }

                        } else {
                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mPage++;
                                mRefreshLayout.setLoadmoreFinished(false);
                            }
                            mAdapter.addData(tmp);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        if (mListBeans == null) {
                            showLoadingDialog();
                        }
                    }
                });
    }

//    @OnClick(R.id.txt_recommend)
//    public void onViewClicked() {
//        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_recommend, null);
//        builder.setView(view);
//        final AlertDialog dialog = builder.create();
//        if (dialog.getWindow() != null) {
//            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        }
//        dialog.show();
//
//        final EditText etName = view.findViewById(R.id.et_name);
//        final EditText etPhone = view.findViewById(R.id.et_phone);
//        TextView txtClose=view.findViewById(R.id.txt_close);
//        TextView txtConfirm=view.findViewById(R.id.txt_confirm);
//
//        txtClose.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialog.dismiss();
//            }
//        });
//        txtConfirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                KeyboardUtil.hideKeyboard(mContext,v);
//                if (isEmpty(etName)||isEmpty(etPhone)) {
//                    showToast("请输入完整信息");
//                    return;
//                }
//                dialog.dismiss();
//                recommend(etName.getText().toString(),etPhone.getText().toString());
//            }
//        });
//    }
//
//    private void recommend(String name, String phone) {
//        showLoadingDialog("正在推荐");
//        Map<String, String> map = new HashMap<>();
//        map.put("sms_title", name);
//        map.put("sms_content", phone);
//        ObservableHelper.createObservable(RetrofitHelper.createService().recommend(map))
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        addDisposable(d);
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        dismissLoadingDialog();
//                        try {
//                            JSONObject object = new JSONObject(s);
//                            switch (object.getString("code")) {
//                                case "200":
//                                    showToast("推荐成功");
//                                    break;
//                                default:
//                                    showToast("推荐失败");
//                                    break;
//                            }
//
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        dismissLoadingDialog();
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//    }
}
